package org.example.myleet.p747;

public class Solution {
    public int dominantIndex(int[] nums) {
        int n = nums.length;
        if (n < 2) {
            return 0;
        }
        int subMax, max, maxId;
        if (nums[0] < nums[1]) {
            max = nums[1];
            maxId = 1;
            subMax = nums[0];
        } else {
            max = nums[0];
            maxId = 0;
            subMax = nums[1];
        }
        for (int i = 2; i < n; ++i) {
            if (nums[i] > max) {
                subMax = max;
                max = nums[i];
                maxId = i;
            } else if (nums[i] > subMax) {
                subMax = nums[i];
            }
        }
        if (max >= 2 * subMax) {
            return maxId;
        }
        return -1;
    }
}
